/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slic.service.impl;

import java.util.Date;
import java.util.List;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;

import slic.equipos.Equipos;
import slic.inscripcion.InscripcionStatus;
import slic.model.Alumno;
import slic.model.Deportista;
import slic.model.NotificacionRechazo;
import slic.service.base.NotificacionRechazoLocalServiceBaseImpl;
import slic.service.persistence.DeportistaUtil;
import slic.service.persistence.NotificacionRechazoUtil;

/**
 * The implementation of the NotificacionRechazo local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slic.service.NotificacionRechazoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Pablo Carballo
 * @see slic.service.base.NotificacionRechazoLocalServiceBaseImpl
 * @see slic.service.NotificacionRechazoLocalServiceUtil
 */
public class NotificacionRechazoLocalServiceImpl
	extends NotificacionRechazoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link slic.service.NotificacionRechazoLocalServiceUtil} to access the NotificacionRechazo local service.
	 */
	
	public NotificacionRechazo agregarNotificacionRechazo(Deportista deportista, long suscriptorUserId) 
			throws SystemException, PortalException {		

		NotificacionRechazo notificacion = createNotificacionRechazo(
				counterLocalService.increment(NotificacionRechazo.class.getName()));
	
		notificacion.setDeportistaId(deportista.getDeportistaId());
		notificacion.setSuscriptorUserId(suscriptorUserId);
		notificacion.setRechazadorUserId(deportista.getInscripcionStatusByUserId());
		notificacion.setCreateDate(deportista.getInscripcionStatusDate());
		notificacion.setComentario(deportista.getInscripcionStatusComment());
		
		return updateNotificacionRechazo(notificacion);
	}
	
	private DynamicQuery getRechazosActualesBySuscriptor(long suscriptorId) throws SystemException {

		DynamicQuery qRechazados = DynamicQueryFactoryUtil.forClass(Deportista.class, "deportista")
			.setProjection(ProjectionFactoryUtil.property("deportista.deportistaId"))
			.add(PropertyFactoryUtil.forName("deportista.inscripcionStatus")
					.eq(InscripcionStatus.STATUS_REJECTED));
		
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(NotificacionRechazo.class, "notificacion")		
				.add(PropertyFactoryUtil.forName("notificacion.suscriptorUserId")
    			.eq(suscriptorId))		
				.add(PropertyFactoryUtil.forName("notificacion.deportistaId")
				.in(qRechazados));
		
		return query;
		
	}
	
	public List<NotificacionRechazo> findRechazosActualesBySuscriptor(long suscriptorId, int start, int end) throws SystemException {
		
		DynamicQuery query = getRechazosActualesBySuscriptor(suscriptorId);		
		// query.addOrder(OrderFactoryUtil.asc("createDate"));
	
		return NotificacionRechazoUtil.findWithDynamicQuery(query, start, end);
		
	}
	
	public long countRechazosActualesBySuscriptor(long suscriptorId) throws SystemException {
		
		DynamicQuery query = getRechazosActualesBySuscriptor(suscriptorId);		
		return 5;
		
		// return new NotificacionRechazoUtil().countWithDynamicQuery(query);
		
	}
	
}